<template>
    <div class="total">
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" label-width="120px"
            class="demo-ruleForm">
            <el-form-item label="昵称" prop="username">
                <el-input v-model.number="ruleForm.username" placeholder="3-10个字母、汉字、数字" />
            </el-form-item>
            <el-form-item label="账号" prop="phone">
                <el-input v-model.number="ruleForm.phone" placeholder="手机号码" />
            </el-form-item>
            <el-form-item label="密码" prop="pass">
                <el-input v-model="ruleForm.pass" type="password" autocomplete="off"
                    placeholder="长度建议在6-16位,至少使用两种字符" />
            </el-form-item>
            <el-form-item label="确认密码" prop="checkPass">
                <el-input v-model="ruleForm.checkPass" type="password" autocomplete="off" placeholder="请再次输入密码" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">确认注册</el-button>
                <el-button @click="resetForm(ruleFormRef)">重置信息</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script lang="ts" setup>

import { reactive, ref } from 'vue'
import type { FormInstance } from 'element-plus'
import { UserService } from '@/utils/api'

const ruleFormRef = ref<FormInstance>()

//数据
const ruleForm = reactive({
    pass: '',
    checkPass: '',
    phone: '',
    username: '',
    avatar: ''
})

//校验账号
const checkPhone = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('账号不能为空'))
    }
    setTimeout(() => {
        if (!/^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$/.test(value)) {
            callback(new Error('请输入正确的手机号码'))
        } else {
            callback()
        }
    }, 1000)
}

//校验昵称
const checkUsername = (rule: any, value: any, callback: any) => {
    if (!value) {
        return callback(new Error('昵称不能为空'))
    }
    setTimeout(() => {
        if (!/^(\w|[\u4e00-\u9fa5]){3,10}$/.test(value)) {
            callback(new Error('用户名格式不正确！'))
        } else {
            callback()
        }
    }, 1000)
}

//校验密码和确认密码
const validatePass = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('密码不能为空'))
    } else {
        if (!/^(\w){6,16}$/.test(value)) {
            callback(new Error('密码格式不正确'))
        } else {
            callback()
            // ruleFormRef.value.validateField('checkPass', () => null)
        }
    }
}
const validatePass2 = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('确认密码不能为空'))
    } else if (value !== ruleForm.pass) {
        callback(new Error("两次输入的密码不匹配"))
    } else {
        callback()
    }
}

//定义校验函数
const rules = reactive({
    pass: [{ validator: validatePass, trigger: 'blur' }],
    checkPass: [{ validator: validatePass2, trigger: 'blur' }],
    phone: [{ validator: checkPhone, trigger: 'blur' }],
    username: [{ validator: checkUsername, trigger: 'blur' }],
})

//提交注册信息
const submitForm = (formEl: FormInstance | undefined) => {

    // UserService.register(ruleForm).then(res => {
    //     console.log(res)
    // })

    UserService.register(ruleForm).then(res => {
        console.log(res)
    })

    // if (!formEl) return
    // formEl.validate((valid) => {
    //     if (valid) {
    //         //信息无误，发送axios请求
    //         const register = () => {
    //             UserService.login2(ruleForm).then((res) => {
    //                 console.log(res)
    //             })
    //         }
    //         console.log('submit!')
    //     } else {
    //         //信息有误
    //         console.log('error submit!')
    //         return false
    //     }
    // })
}

//重置form表单
const resetForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    formEl.resetFields()
}

</script>

<style lang="scss" scoped>
.total {
    width: 50%;
    height: auto;
    position: absolute;
    top: 0.6rem;
    left: 25%;
}
</style>
